package com.sniff.atp.service.purchase;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.sniff.atp.dto.SystemLoginDTO;
import com.sniff.atp.enums.api.PurchaseAPI;
import com.sniff.atp.enums.status.PurchaseAbnormalOperationType;
import com.sniff.atp.enums.status.PurchaseAbnormalType;
import com.sniff.atp.utils.Headers;
import com.sniff.atp.utils.HttpClientUtil;
import com.sniff.atp.utils.JsonUtil;
import com.sniff.atp.utils.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @BelongsProject: sniff-atp
 * @BelongsPackage: com.sniff.atp.service.purchase
 * @Author: zxy
 * @CreateTime: 2022-11-17 11:37
 * @Description: 采购单提交异常
 * @Version: 1.0
 */
@Service
public class PurchaseAbnormalService {
    @Autowired
    private PurchaseService purchaseService;

    @Autowired
    private PurchaseAbnormalOperationService purchaseAbnormalOperationService;


    /**
     * `type` 异常类型:  1 商品确认 2 全网断货 3 原商品缺货 4 预售商品 5 不满足最小起订量 6 更换采购链接 "
     * 7 次品件 8 异常次品件 9 客户退款 10 到错货 11 多件 12 少件 13 扫不出 14 商品下架 15 商品异常',
     */
    public JSONObject applyAbnormalParam(Integer type, Integer abnormalDetailType) {
        JSONObject param = JsonUtil.readJson("purchase/applyAbnormal");
        param.replace("type", type);
        param.replace("abnormalDetailType", abnormalDetailType);

        if (type == PurchaseAbnormalType.PURCHASE_ABNORMAL_TYPE4.getType()) {
            param.remove("abnormalDetailType");
        }
        if (type == PurchaseAbnormalType.PURCHASE_ABNORMAL_TYPE5.getType()) {
            param.put("purchaseUserProductSku", 1);
            param.put("purchaseUserProductLink", "https://detail.1688.com/offer/704320712330.html");
            param.replace("purchaseUserProductSku", "b98c09c9eb538f3e41248aaa9e84fa55");

        }
        if (type == PurchaseAbnormalType.PURCHASE_ABNORMAL_TYPE7.getType()) {
            param.put("remark", "10");
            param.put("purchaseUserProductSku", 1);
            if (abnormalDetailType == PurchaseAbnormalType.PURCHASE_ABNORMAL_TYPE8.getAbnormalDetailType()) {
                param.put("purchaseUserProductLink", "https://detail.1688.com/offer/704320712330.html");
                param.replace("purchaseUserProductSku", "b98c09c9eb538f3e41248aaa9e84fa55");
            }
        }

        if (type == PurchaseAbnormalType.PURCHASE_ABNORMAL_TYPE9.getType()) {
            param.put("miniNumOrderQuantity", "10");
            param.put("remark", "10");
            if (abnormalDetailType == PurchaseAbnormalType.PURCHASE_ABNORMAL_TYPE10.getAbnormalDetailType()) {
                param.put("purchaseUserProductSku", 1);
                param.put("purchaseUserProductLink", "");
            }
        }

        if (type == PurchaseAbnormalType.PURCHASE_ABNORMAL_TYPE11.getType()) {
            param.put("purchaseUserProductSku", "2bd3a20ebca7c8b2bf0583d74e487823");
            param.put("purchaseUserProductLink", "https://detail.1688.com/offer/633471832602.html?spm=a262yb.26503290.kyl8yn4q.32.62c05216Dyk3Ka");
        }

        return param;
    }

    /**
     * 采购提交异常
     */
    public String applyAbnormal(SystemLoginDTO systemLoginDTO, String purchaseId, String purchaseItemId, String purchaseOrderItemRelationId, Integer type, Integer abnormalDetailType) {

        JSONObject param = applyAbnormalParam(type, abnormalDetailType);
        param.replace("purchaseId", purchaseId);
        param.replace("purchaseItemId", purchaseItemId);

        if (type == PurchaseAbnormalType.PURCHASE_ABNORMAL_TYPE9.getType()) {
            param.put("purchaseOrderItemRelationId", purchaseOrderItemRelationId);
            param.remove("purchaseItemId");
        }
        String result = new HttpClientUtil(Headers.setSystemHeader(systemLoginDTO)).doPostJson(PurchaseAPI.APPLY_ABNORMAL.getApi(), param);
        return result;
    }


    /**
     * 提交采购异常流程
     *
     * @param systemLoginDTO
     * @param purchaseNo
     */
    public void userConfirmProduct(SystemLoginDTO systemLoginDTO, String purchaseNo, PurchaseAbnormalType purchaseAbnormalType) {
        purchaseService.setPurchaseToken(systemLoginDTO);


        if (purchaseNo.contains("XSCGD")) {

            String s2 = purchaseService.listPurchase(purchaseNo);
            String purchaseId1 = JsonUtil.extractString(s2, "data.records[0].purchaseId");
            String systemSourceName = JsonUtil.extractString(s2, "data.records[0].systemSourceName");

            purchaseService.editOnlinePurchaseOrder(systemSourceName, 200, purchaseId1);

            String purchaseInfo = purchaseService.purchaseInfo(purchaseId1);
            JSONArray purchaseOrderItemDTOList = JsonUtil.extractJSONArray(purchaseInfo, "data.purchaseOrderItemDTOList");
            for (int i = 0; i < purchaseOrderItemDTOList.size(); i++) {
                JSONObject purchaseOrderItemDTO = purchaseOrderItemDTOList.getJSONObject(i);
                String purchaseItemId = purchaseOrderItemDTO.getString("purchaseItemId");
                Integer sellQuantity = purchaseOrderItemDTO.getInteger("sellQuantity");
                //采购单的子商品，提交异常数量1个
//                purchaseService.editPurchaseItem(String.valueOf(sellQuantity-1), purchaseItemId);
                purchaseService.editPurchaseItem(String.valueOf(sellQuantity / 2), purchaseItemId);

                String purchaseOrderItemRelationId = JsonUtil.extractString(purchaseInfo, "data.purchaseOrderItemDTOList[0].relationRespDTOList[0].purchaseOrderItemRelationId");

                applyAbnormal(systemLoginDTO, purchaseId1, purchaseItemId, purchaseOrderItemRelationId, purchaseAbnormalType.getType(), purchaseAbnormalType.getAbnormalDetailType());
            }
            purchaseService.completeOnlinePurchaseOrder(purchaseId1);
        }

        if (purchaseNo.contains("DKCGD")) {
            String queryOrder = purchaseAbnormalOperationService.queryOrder(systemLoginDTO, purchaseNo);
            String purchaseAbnormalOrderId = JsonUtil.extractString(queryOrder, "data.records[0].purchaseAbnormalOrderRespDTO.purchaseAbnormalOrderId");

            String s1 = purchaseAbnormalOperationService.handleAbnormal(systemLoginDTO, purchaseAbnormalOrderId, PurchaseAbnormalOperationType.USER_CONFIR1);
            LogUtil.info("采购异常单处理返回结果：" + s1);

        }
    }


}
